/**
 * 文件名：@PermissionProcessorMappingSecurityProcessor.java <br/>
 * 包名：cn.efunbox.afw.web.security.processor <br/>
 * 项目名：afw-web <br/>
 * @author xtwin <br/>
 */
package cn.efunbox.afw.web.security.processor;

import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;

import cn.efunbox.afw.core.exception.AfwRuntimeException;
import cn.efunbox.afw.core.security.Permission;

/**
 * 类名：PermissionProcessorMappingSecurityProcessor  <br />
 *
 * 功能：权限处理器映射关系安全处理器
 *
 * @author xtwin <br />
 * 创建时间：2016年8月4日 上午10:31:27  <br />
 * @version 2016年8月4日
 */
public class PermissionProcessorMappingSecurityProcessor extends AbstractPermissionSecurityProcessor {
	
	// 权限与处理器映射关系
	protected Map<String, PermissionProcessor> mapping = Collections.emptyMap();

	/**
	 * 功能: <br/>
	 * 
	 * 重写：xtwin <br/>
	 * 
	 * @version ：2016年8月4日 上午10:31:47<br/>
	 * 
	 * @param permission
	 * @param request
	 * @param response
	 * @param handler
	 * @throws Exception <br/>
	 * @see cn.efunbox.afw.web.security.processor.AbstractPermissionSecurityProcessor#doPermission(cn.efunbox.afw.core.security.Permission, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.web.method.HandlerMethod)
	 */
	@Override
	protected void doPermission(Permission permission, HttpServletRequest request, HttpServletResponse response, HandlerMethod handler) throws Exception {
		for (String value : permission.value()) {
			// 取得处理器
			PermissionProcessor processor = mapping.get(value);
			if (null == processor) {
				throw new AfwRuntimeException("Permission processor is not exist : " + permission.value());
			}
			
			// 执行权限处理
			processor.process(permission, value, request, response, handler);
		}
	}

	/**
	 * @version 2016年8月4日-下午12:03:12
	 */
	public void setMapping(Map<String, PermissionProcessor> mapping) {
		this.mapping = mapping;
	}
}
